package cn.lijigou.shopping;

import java.util.concurrent.ExecutorService;

import cn.lijigou.shopping.alipay.Alipay;
import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnDismissListener;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class AlipayCardActivity extends MActivity{
	private WebView webView;
	private ProgressBar progressBar = null;
	private String url_info = "";
	private String topic = "";
	private TextView payTip = null;
	private Dialog mDialog = null;
	private Button mMyOrder = null;
	private Button mCancle = null;
	
	private TextView back= null;
	private TextView register= null;
	private TextView title_center= null;
	private String shopTitle;
	private ExecutorService executorService= GetSingleThreadPool.getInstance();
	
	private Handler waitHander = new Handler(){
		
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what==0){
				executorService.execute(new Thread() {
					public void run() {
						webView.loadUrl(url_info);
					}
				});
			}else if(msg.what==1){
				showResult();
			};
		}
	};
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);     
		setContentView(R.layout.alipay_card);
		
		ShopDetailActivity.mList.add(this);
		Intent intent = this.getIntent();
		shopTitle = intent.getStringExtra("shopTitle");
		if(intent.hasExtra("url_info")){
			url_info = this.getIntent().getStringExtra("url_info");
			topic = this.getIntent().getStringExtra("topic");
			
			back = (TextView) this.findViewById(R.id.title_btn_left);
			back.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					if(ShopDetailActivity.mList.contains(this)){
						ShopDetailActivity.mList.remove(this);
					}
					AlipayCardActivity.this.finish();
				}
			});
			register = (TextView) this.findViewById(R.id.title_btn_right);
			register.setVisibility(View.GONE);
			title_center = (TextView) this.findViewById(R.id.text_center);
			title_center.setText("银行卡手机支付");
			findViewById(R.id.titleLeftView).setVisibility(View.GONE);
			findViewById(R.id.titleRightView).setVisibility(View.GONE);
			
			webView = (WebView) findViewById(R.id.webview);
			progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
			WebSettings setting = webView.getSettings();
			setting.setBuiltInZoomControls(false);
			setting.setSupportZoom(false);
			setting.setJavaScriptEnabled(true);
			setting.setSupportMultipleWindows(false);
			setting.setLightTouchEnabled(true);
			//优先使用缓存：
			//webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);    
			//不使用缓存：  
			webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
			webView.setBackgroundColor(0); 
			webView.setHorizontalScrollBarEnabled(true);
			webView.setHorizontalScrollbarOverlay(true);
			webView.setVerticalScrollBarEnabled(true);
			webView.setVerticalScrollbarOverlay(true);
			
			webView.requestFocus();
			webView.setDownloadListener(new DownloadListener() {
	            public void onDownloadStart(String url, String userAgent,
	                            String contentDisposition, String mimetype,
	                            long contentLength) {
	                    Uri uri = Uri.parse(url);
	                    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
	                    startActivity(intent);
	            }
			});
			webView.setWebViewClient(new WebViewClient() {
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					return super.shouldOverrideUrlLoading(view, url);
				}
			});
			webView.setWebChromeClient(new WebChromeClient(){
				
				public void onProgressChanged(WebView view, int newProgress) {
					view.getProgress();
					if(newProgress==100){
						progressBar.setVisibility(View.GONE);
					}else{
						progressBar.setVisibility(View.VISIBLE);
						progressBar.setProgress(newProgress);
					}
					super.onProgressChanged(view, newProgress);
				}
			});
			webView.addJavascriptInterface(new Object(){
				//银行卡支付回调
				public void alipay_card() {
					waitHander.sendEmptyMessage(1);
				}
				//保存虚拟账号回调
				public void setUid(String uid,String session) {
					if( MSystem.uid.equals("")){
						SharedPreferences sp = activity.getSharedPreferences("user", Context.MODE_PRIVATE);
						Editor edit = sp.edit();
						edit.putString("uid",uid);
						edit.putString("session",session);
						edit.putString("isTmp","1");
						
						edit.commit();
					}
					
				}
				//支付异常回调
				public void alipay_false() {
					AlipayCardActivity.this.finish();
				}
			},"android");
			waitHander.sendEmptyMessage(0);
		}else{	
			Toast.makeText(this, "订单异常，请您重新下单！", 1).show();
		}
	}
	public void showResult() {
			Intent intent = new Intent(activity, CompleteOrderActivity.class);
			intent.putExtra("shopTitle", shopTitle);
			activity.startActivity(intent);
	}
	
	
	public void setPay(View v1){
		payTip = (TextView) v1.findViewById(R.id.dialog_title);
		payTip.setText("恭喜你购买成功");
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		
		if(webView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK){
//			if(ShopDetail.mList.contains(this)){
//				ShopDetail.mList.remove(this);
//			}
			webView.goBack(); 
			return true;
         }else if(keyCode == KeyEvent.KEYCODE_BACK){
        	 if(ShopDetailActivity.mList.contains(this)){
     			ShopDetailActivity.mList.remove(this);
     		}
        	 this.finish();
         }
      return false;
	}
	
	protected void onDestroy() {
		webView.destroy();
		super.onDestroy();
	}
}
